{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python绘制饼图分析北京天气\n",
    "\n",
    "饼图，是一个划分为几个扇形的圆形统计图表，能够直接以图形的方式直接显示各个组成部分所占比例\n",
    "\n",
    "目的：查看2019年北京天气数据，使用饼图查看天气、风向、空气质量的数据对比"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 编写函数-可以创建一个pyecharts饼图对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Pie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_pie(datas, title) -> Pie:\n",
    "    \"\"\" 创建饼图对象\n",
    "    文档地址：https://pyecharts.org/#/zh-cn/basic_charts?id=pie%ef%bc%9a%e9%a5%bc%e5%9b%be\n",
    "    @param datas: 数据，形式为[('晴', 115), ('多云', 78), ('晴~多云', 39)]\n",
    "    @param title: 图表的标题\n",
    "    \"\"\"\n",
    "    pie = Pie()\n",
    "    pie.add(\"\", datas)\n",
    "    pie.set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=title),\n",
    "        legend_opts=opts.LegendOpts(pos_right=\"right\")\n",
    "    )\n",
    "    pie.set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}: {c}: {d}%\"))\n",
    "    return pie"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 读取北京天气2019年数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./datas/beijing_tianqi/beijing_tianqi_2019.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1℃</td>\n",
       "      <td>-10℃</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>56</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-02</td>\n",
       "      <td>1℃</td>\n",
       "      <td>-9℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>60</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-03</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-7℃</td>\n",
       "      <td>霾</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>165</td>\n",
       "      <td>中度污染</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-04</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-7℃</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-05</td>\n",
       "      <td>0℃</td>\n",
       "      <td>-8℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>29</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd bWendu yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2019-01-01     1℃   -10℃   晴~多云       西北风     1级   56       良         2\n",
       "1  2019-01-02     1℃    -9℃     多云       东北风     1级   60       良         2\n",
       "2  2019-01-03     2℃    -7℃      霾       东北风     1级  165    中度污染         4\n",
       "3  2019-01-04     2℃    -7℃      晴       西北风     2级   50       优         1\n",
       "4  2019-01-05     0℃    -8℃     多云       东北风     2级   29       优         1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 绘制饼图查看天气类型对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tianqi\n",
       "晴         115\n",
       "多云         78\n",
       "晴~多云       39\n",
       "多云~晴       34\n",
       "小雨~多云      11\n",
       "多云~雷阵雨     10\n",
       "霾           8\n",
       "多云~小雨       7\n",
       "雷阵雨         7\n",
       "雷阵雨~多云      7\n",
       "阴~多云        5\n",
       "雷阵雨~中雨      4\n",
       "多云~阴        4\n",
       "小雪~多云       4\n",
       "小雨          4\n",
       "阴~小雨        3\n",
       "多云~中雨       2\n",
       "中雨~雷阵雨      2\n",
       "中雨~小雨       2\n",
       "中雨~多云       2\n",
       "阴           2\n",
       "雷阵雨~小雨      2\n",
       "雷阵雨~晴       2\n",
       "霾~多云        2\n",
       "小雨~晴        1\n",
       "小雨~阴        1\n",
       "小雪          1\n",
       "霾~晴         1\n",
       "晴~霾         1\n",
       "阴~中雨        1\n",
       "多云~中雪       1\n",
       "雾~晴         1\n",
       "霾~雾         1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_tianqi = df.groupby(\"tianqi\").size().sort_values(ascending=False)\n",
    "df_tianqi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('晴', 115),\n",
       " ('多云', 78),\n",
       " ('晴~多云', 39),\n",
       " ('多云~晴', 34),\n",
       " ('小雨~多云', 11),\n",
       " ('多云~雷阵雨', 10),\n",
       " ('霾', 8),\n",
       " ('多云~小雨', 7),\n",
       " ('雷阵雨', 7),\n",
       " ('雷阵雨~多云', 7),\n",
       " ('阴~多云', 5),\n",
       " ('雷阵雨~中雨', 4),\n",
       " ('多云~阴', 4),\n",
       " ('小雪~多云', 4),\n",
       " ('小雨', 4),\n",
       " ('阴~小雨', 3),\n",
       " ('多云~中雨', 2),\n",
       " ('中雨~雷阵雨', 2),\n",
       " ('中雨~小雨', 2),\n",
       " ('中雨~多云', 2),\n",
       " ('阴', 2),\n",
       " ('雷阵雨~小雨', 2),\n",
       " ('雷阵雨~晴', 2),\n",
       " ('霾~多云', 2),\n",
       " ('小雨~晴', 1),\n",
       " ('小雨~阴', 1),\n",
       " ('小雪', 1),\n",
       " ('霾~晴', 1),\n",
       " ('晴~霾', 1),\n",
       " ('阴~中雨', 1),\n",
       " ('多云~中雪', 1),\n",
       " ('雾~晴', 1),\n",
       " ('霾~雾', 1)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datas = list(zip(df_tianqi.index.to_list(), df_tianqi.to_list()))\n",
    "datas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"1db5c6f2b9704f03be522673a11f26bb\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_1db5c6f2b9704f03be522673a11f26bb = echarts.init(\n",
       "                    document.getElementById('1db5c6f2b9704f03be522673a11f26bb'), 'white', {renderer: 'canvas'});\n",
       "                var option_1db5c6f2b9704f03be522673a11f26bb = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u6674\",\n",
       "                    \"value\": 115\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91\",\n",
       "                    \"value\": 78\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6674~\\u591a\\u4e91\",\n",
       "                    \"value\": 39\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91~\\u6674\",\n",
       "                    \"value\": 34\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c0f\\u96e8~\\u591a\\u4e91\",\n",
       "                    \"value\": 11\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91~\\u96f7\\u9635\\u96e8\",\n",
       "                    \"value\": 10\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u973e\",\n",
       "                    \"value\": 8\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91~\\u5c0f\\u96e8\",\n",
       "                    \"value\": 7\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u96f7\\u9635\\u96e8\",\n",
       "                    \"value\": 7\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u96f7\\u9635\\u96e8~\\u591a\\u4e91\",\n",
       "                    \"value\": 7\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u9634~\\u591a\\u4e91\",\n",
       "                    \"value\": 5\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u96f7\\u9635\\u96e8~\\u4e2d\\u96e8\",\n",
       "                    \"value\": 4\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91~\\u9634\",\n",
       "                    \"value\": 4\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c0f\\u96ea~\\u591a\\u4e91\",\n",
       "                    \"value\": 4\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c0f\\u96e8\",\n",
       "                    \"value\": 4\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u9634~\\u5c0f\\u96e8\",\n",
       "                    \"value\": 3\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91~\\u4e2d\\u96e8\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e2d\\u96e8~\\u96f7\\u9635\\u96e8\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e2d\\u96e8~\\u5c0f\\u96e8\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e2d\\u96e8~\\u591a\\u4e91\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u9634\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u96f7\\u9635\\u96e8~\\u5c0f\\u96e8\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u96f7\\u9635\\u96e8~\\u6674\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u973e~\\u591a\\u4e91\",\n",
       "                    \"value\": 2\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c0f\\u96e8~\\u6674\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c0f\\u96e8~\\u9634\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5c0f\\u96ea\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u973e~\\u6674\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6674~\\u973e\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u9634~\\u4e2d\\u96e8\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u591a\\u4e91~\\u4e2d\\u96ea\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u96fe~\\u6674\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u973e~\\u96fe\",\n",
       "                    \"value\": 1\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c}: {d}%\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u6674\",\n",
       "                \"\\u591a\\u4e91\",\n",
       "                \"\\u6674~\\u591a\\u4e91\",\n",
       "                \"\\u591a\\u4e91~\\u6674\",\n",
       "                \"\\u5c0f\\u96e8~\\u591a\\u4e91\",\n",
       "                \"\\u591a\\u4e91~\\u96f7\\u9635\\u96e8\",\n",
       "                \"\\u973e\",\n",
       "                \"\\u591a\\u4e91~\\u5c0f\\u96e8\",\n",
       "                \"\\u96f7\\u9635\\u96e8\",\n",
       "                \"\\u96f7\\u9635\\u96e8~\\u591a\\u4e91\",\n",
       "                \"\\u9634~\\u591a\\u4e91\",\n",
       "                \"\\u96f7\\u9635\\u96e8~\\u4e2d\\u96e8\",\n",
       "                \"\\u591a\\u4e91~\\u9634\",\n",
       "                \"\\u5c0f\\u96ea~\\u591a\\u4e91\",\n",
       "                \"\\u5c0f\\u96e8\",\n",
       "                \"\\u9634~\\u5c0f\\u96e8\",\n",
       "                \"\\u591a\\u4e91~\\u4e2d\\u96e8\",\n",
       "                \"\\u4e2d\\u96e8~\\u96f7\\u9635\\u96e8\",\n",
       "                \"\\u4e2d\\u96e8~\\u5c0f\\u96e8\",\n",
       "                \"\\u4e2d\\u96e8~\\u591a\\u4e91\",\n",
       "                \"\\u9634\",\n",
       "                \"\\u96f7\\u9635\\u96e8~\\u5c0f\\u96e8\",\n",
       "                \"\\u96f7\\u9635\\u96e8~\\u6674\",\n",
       "                \"\\u973e~\\u591a\\u4e91\",\n",
       "                \"\\u5c0f\\u96e8~\\u6674\",\n",
       "                \"\\u5c0f\\u96e8~\\u9634\",\n",
       "                \"\\u5c0f\\u96ea\",\n",
       "                \"\\u973e~\\u6674\",\n",
       "                \"\\u6674~\\u973e\",\n",
       "                \"\\u9634~\\u4e2d\\u96e8\",\n",
       "                \"\\u591a\\u4e91~\\u4e2d\\u96ea\",\n",
       "                \"\\u96fe~\\u6674\",\n",
       "                \"\\u973e~\\u96fe\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"right\": \"right\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u997c\\u56fe-\\u5929\\u6c14\\u5bf9\\u6bd4\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_1db5c6f2b9704f03be522673a11f26bb.setOption(option_1db5c6f2b9704f03be522673a11f26bb);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x28e8844ef88>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pie = create_pie(datas, \"饼图-天气对比\")\n",
    "pie.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 绘制饼图查看风向数据比例对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1℃</td>\n",
       "      <td>-10℃</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>56</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-02</td>\n",
       "      <td>1℃</td>\n",
       "      <td>-9℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>60</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-03</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-7℃</td>\n",
       "      <td>霾</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>165</td>\n",
       "      <td>中度污染</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-04</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-7℃</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-05</td>\n",
       "      <td>0℃</td>\n",
       "      <td>-8℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>29</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd bWendu yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2019-01-01     1℃   -10℃   晴~多云       西北风     1级   56       良         2\n",
       "1  2019-01-02     1℃    -9℃     多云       东北风     1级   60       良         2\n",
       "2  2019-01-03     2℃    -7℃      霾       东北风     1级  165    中度污染         4\n",
       "3  2019-01-04     2℃    -7℃      晴       西北风     2级   50       优         1\n",
       "4  2019-01-05     0℃    -8℃     多云       东北风     2级   29       优         1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"15b013b2991f4292bf9603c891663cbb\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_15b013b2991f4292bf9603c891663cbb = echarts.init(\n",
       "                    document.getElementById('15b013b2991f4292bf9603c891663cbb'), 'white', {renderer: 'canvas'});\n",
       "                var option_15b013b2991f4292bf9603c891663cbb = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u4e1c\\u5357\\u98ce\",\n",
       "                    \"value\": 104\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u897f\\u5317\\u98ce\",\n",
       "                    \"value\": 100\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u897f\\u5357\\u98ce\",\n",
       "                    \"value\": 81\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e1c\\u5317\\u98ce\",\n",
       "                    \"value\": 78\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5357\\u98ce\",\n",
       "                    \"value\": 1\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5317\\u98ce\",\n",
       "                    \"value\": 1\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c}: {d}%\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u4e1c\\u5357\\u98ce\",\n",
       "                \"\\u897f\\u5317\\u98ce\",\n",
       "                \"\\u897f\\u5357\\u98ce\",\n",
       "                \"\\u4e1c\\u5317\\u98ce\",\n",
       "                \"\\u5357\\u98ce\",\n",
       "                \"\\u5317\\u98ce\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"right\": \"right\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u997c\\u56fe-\\u98ce\\u5411\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_15b013b2991f4292bf9603c891663cbb.setOption(option_15b013b2991f4292bf9603c891663cbb);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x28e88475d48>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_fengxiang = df.groupby(\"fengxiang\").size().sort_values(ascending=False)\n",
    "datas = list(zip(df_fengxiang.index.to_list(), df_fengxiang.to_list()))\n",
    "pie = create_pie(datas, \"饼图-风向\")\n",
    "pie.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 绘制饼图查看空气质量对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-01</td>\n",
       "      <td>1℃</td>\n",
       "      <td>-10℃</td>\n",
       "      <td>晴~多云</td>\n",
       "      <td>西北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>56</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-02</td>\n",
       "      <td>1℃</td>\n",
       "      <td>-9℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>60</td>\n",
       "      <td>良</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-03</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-7℃</td>\n",
       "      <td>霾</td>\n",
       "      <td>东北风</td>\n",
       "      <td>1级</td>\n",
       "      <td>165</td>\n",
       "      <td>中度污染</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-04</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-7℃</td>\n",
       "      <td>晴</td>\n",
       "      <td>西北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>50</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-05</td>\n",
       "      <td>0℃</td>\n",
       "      <td>-8℃</td>\n",
       "      <td>多云</td>\n",
       "      <td>东北风</td>\n",
       "      <td>2级</td>\n",
       "      <td>29</td>\n",
       "      <td>优</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd bWendu yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2019-01-01     1℃   -10℃   晴~多云       西北风     1级   56       良         2\n",
       "1  2019-01-02     1℃    -9℃     多云       东北风     1级   60       良         2\n",
       "2  2019-01-03     2℃    -7℃      霾       东北风     1级  165    中度污染         4\n",
       "3  2019-01-04     2℃    -7℃      晴       西北风     2级   50       优         1\n",
       "4  2019-01-05     0℃    -8℃     多云       东北风     2级   29       优         1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"77682436481247b4a186568138e7ea12\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_77682436481247b4a186568138e7ea12 = echarts.init(\n",
       "                    document.getElementById('77682436481247b4a186568138e7ea12'), 'white', {renderer: 'canvas'});\n",
       "                var option_77682436481247b4a186568138e7ea12 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u826f\",\n",
       "                    \"value\": 186\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4f18\",\n",
       "                    \"value\": 114\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8f7b\\u5ea6\\u6c61\\u67d3\",\n",
       "                    \"value\": 47\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u4e2d\\u5ea6\\u6c61\\u67d3\",\n",
       "                    \"value\": 13\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u91cd\\u5ea6\\u6c61\\u67d3\",\n",
       "                    \"value\": 5\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c}: {d}%\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u826f\",\n",
       "                \"\\u4f18\",\n",
       "                \"\\u8f7b\\u5ea6\\u6c61\\u67d3\",\n",
       "                \"\\u4e2d\\u5ea6\\u6c61\\u67d3\",\n",
       "                \"\\u91cd\\u5ea6\\u6c61\\u67d3\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"right\": \"right\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u997c\\u56fe-\\u7a7a\\u6c14\\u8d28\\u91cf\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_77682436481247b4a186568138e7ea12.setOption(option_77682436481247b4a186568138e7ea12);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x28e884b4048>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_aqiInfo = df.groupby(\"aqiInfo\").size().sort_values(ascending=False)\n",
    "datas = list(zip(df_aqiInfo.index.to_list(), df_aqiInfo.to_list()))\n",
    "pie = create_pie(datas, \"饼图-空气质量\")\n",
    "pie.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
